home *** CD-ROM | disk | FTP | other *** search
/ Internet.Works 36 / Issue 36.iso / pc / iwks / Wap / NokiaToolkit2_0 / setup.exe / Disk1 / data1.cab / Push_Samples / StockServlet.java < prev   
Encoding:
Java Source  |  2000-06-19  |  2.4 KB  |  102 lines

  1. /**
  2.  * StockServlet receives the customer "id" on the
  3.  * request parameter list. It queries the
  4.  * customer database to get the list of stocks, then
  5.  * queries the stock database for the price
  6.  * of each stock. It then formats this info as WML.
  7.  *
  8.  * Note: Only the GET command is supported. No error
  9.  * checking is included in this listing.
  10.  *
  11.  * From the Stock Quote Push Application chapter in the Nokia
  12.  * Developer's Guide for the WAP Toolkit.
  13.  */
  14.  
  15. import java.io.*;
  16. import java.util.*;
  17. import javax.servlet.*;
  18. import javax.servlet.http.*;
  19.  
  20. public class StockServlet extends HttpServlet {
  21.     // throws ServletException, IOTException {
  22.  
  23.     static private String TITLE = "Today's Stock Quotes";
  24.  
  25.     public void doGet(HttpServletRequest req, HttpServletResponse res) {
  26.     //
  27.     // Set response content type
  28.     //
  29.     res.setContentType("text/vnd.wap.wml");
  30.         //
  31.     // Get info from request
  32.     //
  33.     String id = req.getParameter("id");
  34.     PrintWriter out = null;
  35.     try { 
  36.         out = res.getWriter();
  37.     } catch (Exception e) {
  38.         return;
  39.     }
  40.     //
  41.     // Query customer database for list of favorite stocks
  42.     //
  43.     Customer cust = CustomerDatabase.get(id);
  44.     Vector list = cust.getStocks();
  45.     //
  46.     // Print WML preface, title, and table header
  47.     //
  48.     out.println("<wml><deck><card>");
  49.     out.println( TITLE );
  50.     //
  51.     // start table
  52.     //
  53.     out.println( "<table columns=\"2\">");
  54.     //
  55.     // table header
  56.     //
  57.     out.println("<tr><td><b>Company</td>");
  58.     out.println("<td><b>Stock Price</b></td></tr>");
  59.     //
  60.     // Iterate through stock list and query stock price
  61.     // database for current price.
  62.     //
  63.     for (int i =0;i < list.size(); i++ ) {
  64.         String price = StockInfoDB.getPrice( (String)list.elementAt(i));
  65.         out.println( "<tr>");
  66.         out.println( "<td>" + list.elementAt(i) + "</td>");
  67.         out.println( "<td>" + price + "</td>");
  68.         out.println( "</tr>");
  69.     }
  70.     //
  71.     // Finish WML page.
  72.     //
  73.     out.println("</table></card></deck></wml>");
  74.     }
  75. }
  76.  
  77. /*
  78.  * These are stub classes which would provide stock database info
  79.  * and track customer info.
  80.  */
  81. class Customer {
  82.     private String id;
  83.     public Customer(String id) {
  84.         this.id = id;
  85.     }
  86.     public Vector getStocks() {
  87.         Vector v = new Vector(1);
  88.         v.add("FooCo");
  89.         return v;
  90.     }
  91. }
  92. class CustomerDatabase {
  93.     public static Customer get (String id) {
  94.         return new Customer(id);
  95.     }
  96. }
  97. class StockInfoDB {
  98.     public static String getPrice( String name ) {
  99.         return "Stock price of " + name;
  100.     }
  101. }
  102.